<template>
  <goods-tree @input="getList">

  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="货号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入货号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布" prop="publish">
        <el-select v-model="queryParams.publish" placeholder="请选择发布" clearable size="small">
          <el-option
            v-for="dict in publishOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"  :columns="columns"  @dragTable="setToobarColumns" :storageKey="'FieldToolBar:@/api/product/goods'"></right-toolbar>
    </el-row>
     <el-table border v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
       <template v-for="(columnRef,indexRef) in columnsOut">
         <el-table-column type="selection" width="55" align="center"  v-if="columnRef.name==='$selection'" :key="columnRef.name"  />
      <el-table-column label="货号" align="center" prop="no" :show-overflow-tooltip="true"  v-if="columnRef.name==='no'"  :key="columnRef.name" />
      <el-table-column label="店铺目录" align="center" prop="did" :show-overflow-tooltip="true"  v-if="columnRef.name==='did'"  :key="columnRef.name" />
      <el-table-column label="产品目录" align="center" prop="cid" :show-overflow-tooltip="true"  v-if="columnRef.name==='cid'"  :key="columnRef.name" />
      <el-table-column label="品牌" align="center" prop="brand" :show-overflow-tooltip="true"  v-if="columnRef.name==='brand'"  :key="columnRef.name" />
      <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true"  v-if="columnRef.name==='title'"  :key="columnRef.name" />
      <el-table-column label="副标题" align="center" prop="subtitle" :show-overflow-tooltip="true"  v-if="columnRef.name==='subtitle'"  :key="columnRef.name" />
      <el-table-column label="库存" align="center" prop="quanity" :show-overflow-tooltip="true"  v-if="columnRef.name==='quanity'"  :key="columnRef.name" />
      <el-table-column label="单价" align="center" prop="price" :show-overflow-tooltip="true"  v-if="columnRef.name==='price'"  :key="columnRef.name" />
      <el-table-column label="折扣" align="center" prop="discount" :show-overflow-tooltip="true"  v-if="columnRef.name==='discount'"  :key="columnRef.name" />
      <el-table-column label="产地" align="center" prop="origin" :show-overflow-tooltip="true"  v-if="columnRef.name==='origin'"  :key="columnRef.name" />
      <el-table-column label="规格" align="center" prop="specifications" :show-overflow-tooltip="true"  v-if="columnRef.name==='specifications'"  :key="columnRef.name" />
      <el-table-column label="保质期小时" align="center" prop="expiration" :show-overflow-tooltip="true"  v-if="columnRef.name==='expiration'"  :key="columnRef.name" />
      <el-table-column label="保鲜方式" align="center" prop="freshKeeping" :show-overflow-tooltip="true"  v-if="columnRef.name==='freshKeeping'"  :key="columnRef.name" />
      <el-table-column label="发布" align="center" prop="publish" :formatter="publishFormat" :show-overflow-tooltip="true"  v-if="columnRef.name==='publish'"  :key="columnRef.name" />
      <el-table-column width="200"  label="操作" align="center" class-name="small-padding fixed-width" v-if="columnRef.name==='$operation'" :key="columnRef.name" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column >
       </template>
     </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :closeOnClickModal="false">
      <el-form  :key="'addForm'"  v-if="addOrUpdate==='add'" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="货号" prop="no" >
          <el-input v-model="form.no" placeholder="请输入货号" />
        </el-form-item>
        <el-form-item label="产品目录" prop="cid" >
          <el-input v-model="form.cid" placeholder="请输入产品目录" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand" >
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="标题" prop="title" >
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="副标题" prop="subtitle" >
          <el-input v-model="form.subtitle" placeholder="请输入副标题" />
        </el-form-item>
        <el-form-item label="库存" prop="quanity" >
          <el-input v-model="form.quanity" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="单价" prop="price" >
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="折扣" prop="discount" >
          <el-input v-model="form.discount" placeholder="请输入折扣" />
        </el-form-item>
        <el-form-item label="产地" prop="origin" >
          <el-input v-model="form.origin" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="规格" prop="specifications" >
          <el-input v-model="form.specifications" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="保质期小时" prop="expiration" >
          <el-input v-model="form.expiration" placeholder="请输入保质期小时" />
        </el-form-item>
        <el-form-item label="保鲜方式" prop="freshKeeping" >
          <el-input v-model="form.freshKeeping" placeholder="请输入保鲜方式" />
        </el-form-item>
        <el-form-item label="发布" prop="publish">
          <el-select v-model="form.publish" placeholder="请选择发布">
            <el-option
              v-for="dict in publishOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <el-form  :key="'updateForm'"  v-if="addOrUpdate==='update'" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="货号" prop="no" >
          <el-input v-model="form.no" placeholder="请输入货号" />
        </el-form-item>
        <el-form-item label="店铺目录" prop="did" >
          <el-input v-model="form.did" placeholder="请输入店铺目录" />
        </el-form-item>
        <el-form-item label="产品目录" prop="cid" >
          <el-input v-model="form.cid" placeholder="请输入产品目录" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand" >
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="标题" prop="title" >
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="副标题" prop="subtitle" >
          <el-input v-model="form.subtitle" placeholder="请输入副标题" />
        </el-form-item>
        <el-form-item label="库存" prop="quanity" >
          <el-input v-model="form.quanity" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="单价" prop="price" >
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="折扣" prop="discount" >
          <el-input v-model="form.discount" placeholder="请输入折扣" />
        </el-form-item>
        <el-form-item label="产地" prop="origin" >
          <el-input v-model="form.origin" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="规格" prop="specifications" >
          <el-input v-model="form.specifications" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="保质期小时" prop="expiration" >
          <el-input v-model="form.expiration" placeholder="请输入保质期小时" />
        </el-form-item>
        <el-form-item label="保鲜方式" prop="freshKeeping" >
          <el-input v-model="form.freshKeeping" placeholder="请输入保鲜方式" />
        </el-form-item>
        <el-form-item label="发布" prop="publish">
          <el-select v-model="form.publish" placeholder="请选择发布">
            <el-option
              v-for="dict in publishOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

  </goods-tree>
</template>

<script>
import { listGoods, getGoods, exportGoods,delGoods, addGoods, updateGoods } from "@/api/product/goods";
import {initDrag} from "@/utils/dragtable";
import {  TreeGoods } from "@/views/product/category/tree";

export default {
  name: "Goods",
  components: {GoodsTree
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层类型
      addOrUpdate: 'add',
      // 发布字典
      publishOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        title: null,
        publish: null,
      },
      // 表单参数
      form: {},
      columnsOut:[],
      // 列信息
      columns: [
          { key: 0, label: `选择`, visible: true ,name:'$selection',sort:0},
          { key: 1, label: `货号`, visible: true ,name:'no',sort:1},
          { key: 2, label: `店铺目录`, visible: true ,name:'did',sort:2},
          { key: 3, label: `产品目录`, visible: true ,name:'cid',sort:3},
          { key: 4, label: `品牌`, visible: true ,name:'brand',sort:4},
          { key: 5, label: `标题`, visible: true ,name:'title',sort:5},
          { key: 6, label: `副标题`, visible: true ,name:'subtitle',sort:6},
          { key: 7, label: `库存`, visible: true ,name:'quanity',sort:7},
          { key: 8, label: `单价`, visible: true ,name:'price',sort:8},
          { key: 9, label: `折扣`, visible: true ,name:'discount',sort:9},
          { key: 10, label: `产地`, visible: true ,name:'origin',sort:10},
          { key: 11, label: `规格`, visible: true ,name:'specifications',sort:11},
          { key: 12, label: `保质期小时`, visible: true ,name:'expiration',sort:12},
          { key: 13, label: `保鲜方式`, visible: true ,name:'freshKeeping',sort:13},
          { key: 14, label: `发布`, visible: true ,name:'publish',sort:14},
          { key: 15, label: `操作`, visible: true ,name:'$operation',sort:15}
      ],
      // 表单校验
      rules: {
        cid: [
          { required: true, pattern:/^[0-9.]+$/,  message: "产品目录不能为空", trigger: "change" }
        ],
        title: [
          { required: true,   message: "标题不能为空", trigger: "change" }
        ],
        quanity: [
          { required: true, pattern:/^[0-9.]+$/, message: "库存不能为空", trigger: "change" }
        ],
        price: [
          { required: true,  pattern:/^[0-9.]+$/, message: "单价不能为空", trigger: "change" }
        ],
        discount: [
          { required: true,  pattern:/^[0-9.]+$/, message: "折扣不能为空", trigger: "change" }
        ],
        publish: [
          { required: true,   message: "发布不能为空", trigger: "change" }
        ],
      }
    };
  },
  mounted() {
    initDrag(this);
  },
  created() {
    this.getList();
    this.getDicts("goods.n_publish").then(response => {
      this.publishOptions = response.data;
    });
  },
  methods: {
      setToobarColumns(columns){
          this.columnsOut=columns;
      },

      /** 查询商品列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 发布字典翻译
    publishFormat(row, column) {
      return this.selectDictLabel(this.publishOptions, row.publish);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mid: null,
        no: null,
        did: null,
        cid: null,
        brand: null,
        title: null,
        subtitle: null,
        quanity: null,
        price: null,
        discount: null,
        origin: null,
        specifications: null,
        expiration: null,
        freshKeeping: null,
        publish: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.addOrUpdate="add";
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
          this.addOrUpdate="update";
          this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商品编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGoods(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
          const queryParams = this.queryParams;
          this.$confirm('是否确认导出所有商品数据项?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
          }).then(function() {
              return exportGoods(queryParams);
          }).then(response => {
              this.download(response.msg);
          })
     },




















  }
};
</script>
